#!/usr/bin/env python

__version__="0.1"
__author__="Karl Norby <knorby{at}uchicago{dot}edu>"

import os, warnings
from filters import fileFilter
import languages

def getFileExtensions(filename):
    name, extension = os.path.splitext(filename)
    extensions = [extension]
    while (not extension==''):
        name, extension = os.path.splitext(name)
        if extension=='': return extensions
        extensions.append(extension + extensions[-1:])
    return None

#==============================================================================
#
class PackageFile(object):
    #==========================================================================
    #
    def __init__(self, license, rootPath, packagePath, filename, fileType=None):
        self.license     = license
        self.rootPath    = rootPath
        self.packagePath = packagePath
        self.filename    = filename
        self.fullPath    = os.path.join(self.rootPath, self.packagePath, self.filename)
        self.isProgFile  = None #boolean once initialized
        self.fileType    = fileType
        self.determineFileType()
    #==========================================================================
    #
    def determineFileType(self):
        if self.isFileType==None:
            return
        fileExtensions = getFileExtensions(self.filename)
        if len(fileExtensions)>0:
            getFileType(fileExtensions, self.filename)

#==============================================================================
#
class PackageMap(object):
    #==========================================================================
    #
    def __init__(self, defaultLicense, rootPath, path=""):
        self.defaultLicense = defaultLicense
        self.path           = path
        self.rootPath       = rootPath
        self.dirPath        = os.path.join(self.rootPath, path)
        self.shortFilename  = os.path.join(path, "packagemap.xml")
        self.filename       = os.path.join(self.rootPath, self.shortFilename)
        self.incDirs        = []
        self.incFiles       = []
        self.processDirectoryListing()
    #==========================================================================
    #
    def processDirectoryListing(self):
        listing = fileFilter(os.listdir(self.dirPath))
        for item in listing:
            if os.path.isfile(item):
                self.handleFile(item)
            elif os.path.isdir(item):
                self.handleDir(item)
            elif os.path.islink(item):
                warnings.warn("'" + os.path.join(self.dirPath, item) + "' is a link. Links are currently not supported.")
            else:
                warnings.warn("'" + os.path.join(self.dirPath, item) + "' is not a file or directory")
    #==========================================================================
    #                
    def handleDir(self, relativeDirName):
        defaultLicense = getDirectoryLicense(self.defaultLicense)
        packageMap     = PackageMap(defaultLicense, self.rootPath, os.path.join(self.path, relativeDirName))
        if len(packageMap.incDirs)>0 or len(packageMap.incFiles)>0:
            self.incDirs.append(packageMap)
    #==========================================================================
    #
    def handleFile(self, relativeFileName):
        fileLicense = getFileLicense(self.defaultLicense)
        filePackage = FilePackage(fileLicense, self.rootPath, self.path, relativeFileName)
        
